package com.xmjs.baselibrary.view.recyclerview.pull;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

/**
 * 刷新基类
 */

public abstract class AbRefreshView extends LinearLayout {
    protected Context mContext;
    protected View mMainView;

    public AbRefreshView(Context context) {
        this(context, null);
    }

    public AbRefreshView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AbRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    protected void init(Context context) {
        mContext = context;
        mMainView = onCreateView(context);
        initView();
        addView(mMainView);
    }

    //创建布局
    protected abstract View onCreateView(Context context);

    //初始化控件
    protected abstract void initView();

    /**
     * 获取控件最小宽度
     * @return
     */
    public int getMinWidth() {
        return mMainView.getMeasuredWidth();
    }

    /**
     * 获取控件最小高度
     * @return
     */
    public int getMinHeight() {
        return mMainView.getMeasuredHeight();
    }

    /**
     * 获取控件最大可拉动的高度
     *
     * @return
     */
    public int getMaxPullHeight() {
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        int height = wm.getDefaultDisplay().getHeight();
        return height;
    }

    /**
     * 获取控件最大可拉动的宽度（针对水平拉动刷新）
     *
     * @return
     */
    public int getMaxPullWidth() {
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        return width;
    }

    /**
     * 初始化控件
     *
     * @param viewId
     * @return
     */
    protected final View findViewFromId(int viewId) {
        return mMainView.findViewById(viewId);
    }

    /**
     * 获取可见高度
     *
     * @return
     */
    public int getVisibleHeight() {
        LayoutParams lp = (LayoutParams) mMainView.getLayoutParams();
        return lp.height;
    }

    /**
     * 获取可见宽度
     *
     * @return
     */
    public int getVisibleWidth() {
        LayoutParams lp = (LayoutParams) mMainView.getLayoutParams();
        return lp.width;
    }

    /**
     * 动态滚动
     *
     * @param destHeight
     * @param scollListener
     */
    protected void smoothScrollTo(int destHeight, ScrollListener scollListener) {
        ValueAnimator animator = ValueAnimator.ofInt(getVisibleHeight(), destHeight);
        animator.setDuration(300).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                setVisibleHeight((int) animation.getAnimatedValue());
                if (scollListener != null)
                    scollListener.onPull((int) animation.getAnimatedValue());
            }
        });
        animator.start();
    }

    /**
     * 设置可见高度
     *
     * @param height
     */
    public void setVisibleHeight(int height) {
        if (height < 0) {
            height = 0;
        }
        LayoutParams lp = (LayoutParams) mMainView.getLayoutParams();
        lp.height = height;
        mMainView.setLayoutParams(lp);
    }

    /**
     * 设置可见宽度
     *
     * @param width
     */
    public void setVisibleWidth(int width) {
        if (width < 0) {
            width = 0;
        }
        LayoutParams lp = (LayoutParams) mMainView.getLayoutParams();
        lp.width = width;
        mMainView.setLayoutParams(lp);
    }
}
